package cn.rdq.iknessplatform.service.impl;

import cn.rdq.api.po.Catelog;
import cn.rdq.api.vo.CatelogVo;
import cn.rdq.common.constant.CatelogDefault;
import cn.rdq.dao.CatelogMapper;
import cn.rdq.iknessplatform.service.CatelogDramaService;
import cn.rdq.iknessplatform.service.CatelogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CatelogServiceImpl extends ServiceImpl<CatelogMapper, Catelog> implements CatelogService {

    @Autowired
    private CatelogDramaService catelogDramaService;

    @Override
    public boolean updateCatelogIntro(Catelog catelog) {
        UpdateWrapper<Catelog> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", catelog.getId())
                .set("title", catelog.getTitle())
                .set("cover", catelog.getCover())
                .set("description", catelog.getDescription());
        return update(wrapper);
    }

    @Override
    public List<Catelog> catelogList(CatelogVo catelogVo) {
        PageHelper.startPage(catelogVo.getCurrentPage(), catelogVo.getPageSize());
        return list();
    }

    @Override
    public List<Catelog> pageByUserId(CatelogVo catelogVo) {
        PageHelper.startPage(catelogVo.getCurrentPage(), catelogVo.getPageSize());
        return listByUserId(catelogVo.getUserId());
    }

    @Override
    public List<Catelog> listByUserId(Long userId) {
        QueryWrapper<Catelog> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return list(wrapper);
    }

    @Override
    public boolean deleteCatelogById(Long id) {
        removeById(id);

        catelogDramaService.deleteByCatelog(id);
        return true;
    }

    @Override
    public boolean createDefaultCatelog(Catelog catelog) {
        catelog.setDescription(CatelogDefault.NON_DESCRIPTION);
        catelog.setCover(CatelogDefault.COVER);
        return save(catelog);
    }
}
